﻿using MCPlatform.UI.Controls.Models;
using Panuon.WPF.UI;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace MCPlatform.UI.Controls.Views
{
    /// <summary>
    /// Tray.xaml 的交互逻辑
    /// </summary>
    public partial class Tray : UserControl
    {


        public ObservableCollection<TrayModel> TrayItems
        {
            get { return (ObservableCollection<TrayModel>)GetValue(TrayItemsProperty); }
            set { SetValue(TrayItemsProperty, value); }
        }

        // Using a DependencyProperty as the backing store for TrayItems.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty TrayItemsProperty =
            DependencyProperty.Register("TrayItems", typeof(ObservableCollection<TrayModel>), typeof(Tray), new PropertyMetadata(default));



        private bool isMouseButtonDown;
        private Point startPoint;
        private Border? currentBoxSelectedBorder = null;

        public Tray()
        {
            InitializeComponent();
          
           
           
        }

        
       

       

        #region properties





        public int RowCount
        {
            get { return (int)GetValue(RowCountProperty); }
            set { SetValue(RowCountProperty, value); }
        }

        // Using a DependencyProperty as the backing store for RowCount.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty RowCountProperty =
            DependencyProperty.Register("RowCount", typeof(int), typeof(Tray), new PropertyMetadata(default));




        public int ColumnCount
        {
            get { return (int)GetValue(ColumnCountProperty); }
            set { SetValue(ColumnCountProperty, value); }
        }

        // Using a DependencyProperty as the backing store for ColumnCount.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ColumnCountProperty =
            DependencyProperty.Register("ColumnCount", typeof(int), typeof(Tray), new PropertyMetadata(default));






        #endregion

        #region Methods

        

       

        private void canvas_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            isMouseButtonDown = true;
            startPoint = e.GetPosition(canvas);
        }

        private void canvas_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            if (currentBoxSelectedBorder != null)
            {
                //获取选框的矩形位置
                Point tempEndPoint = e.GetPosition(this.canvas);
                Rect tempRect = new Rect(startPoint, tempEndPoint);
                //获取子控件
                List<Rectangle> childList = GetChildObjects<Rectangle>(this.canvas);
                foreach (var child in childList)
                {
                    // 假设child是你的Rectangle控件
                    Rect childRect = new Rect(0, 0, child.Width, child.Height);

                    // 获取child在Canvas中的坐标
                    GeneralTransform transform = child.TransformToAncestor(canvas);
                    childRect.Location = transform.Transform(new Point(0, 0));

                    //若子控件与选框相交则改变样式
                    if (childRect.IntersectsWith(tempRect))
                    {                      
                        var clickedTrayItem = child.DataContext as TrayModel;
                        if (clickedTrayItem is null) { return; }
                        clickedTrayItem.Value = clickedTrayItem.Value == 1 ? 0 : 1;

                        // 获取在TrayItems中的索引
                        int index = TrayItems.IndexOf(clickedTrayItem);
                        
                      
                    }

                }
                this.canvas.Children.Remove(currentBoxSelectedBorder);
                currentBoxSelectedBorder = null;
            }

            isMouseButtonDown = false;
        }

        private void canvas_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
        {
            isMouseButtonDown = true;
            startPoint = e.GetPosition(canvas);
        }

        private void canvas_MouseRightButtonUp(object sender, MouseButtonEventArgs e)
        {
            if (currentBoxSelectedBorder != null)
            {
                //获取选框的矩形位置
                Point tempEndPoint = e.GetPosition(this.canvas);
                Rect tempRect = new Rect(startPoint, tempEndPoint);
                //获取子控件
                List<Rectangle> childList = GetChildObjects<Rectangle>(this.canvas);
                foreach (var child in childList)
                {
                    // 假设child是你的Rectangle控件
                    Rect childRect = new Rect(0, 0, child.Width, child.Height);

                    // 获取child在Canvas中的坐标
                    GeneralTransform transform = child.TransformToAncestor(canvas);
                    childRect.Location = transform.Transform(new Point(0, 0));

                    //若子控件与选框相交则改变样式
                    if (childRect.IntersectsWith(tempRect))
                    {
                        var clickedTrayItem = child.DataContext as TrayModel;
                        if (clickedTrayItem is null) { return; }                      
                        clickedTrayItem.Value = clickedTrayItem.Value == 2 ? 0 : 2;

                        // 获取在TrayItems中的索引
                        int index = TrayItems.IndexOf(clickedTrayItem);


                    }

                }
                this.canvas.Children.Remove(currentBoxSelectedBorder);
                currentBoxSelectedBorder = null;
            }

            isMouseButtonDown = false;
        }
        private void canvas_MouseMove(object sender, MouseEventArgs e)
        {
            if (isMouseButtonDown)
            {
                Point tempEndPoint = e.GetPosition(this.canvas);
                //绘制跟随鼠标移动的方框
                DrawMultiselectBorder(tempEndPoint, startPoint);
            }
        }
       
       
        private void DrawMultiselectBorder(Point endPoint, Point startPoint)
        {
            if (currentBoxSelectedBorder == null)
            {
                currentBoxSelectedBorder = new Border();
                currentBoxSelectedBorder.Background = new SolidColorBrush(Colors.Orange);
                currentBoxSelectedBorder.Opacity = 0.4;
                currentBoxSelectedBorder.BorderThickness = new Thickness(2);
                currentBoxSelectedBorder.BorderBrush = new SolidColorBrush(Colors.OrangeRed);
                Canvas.SetZIndex(currentBoxSelectedBorder, 100);
                this.canvas.Children.Add(currentBoxSelectedBorder);
            }
            currentBoxSelectedBorder.Width = Math.Abs(endPoint.X - startPoint.X);
            currentBoxSelectedBorder.Height = Math.Abs(endPoint.Y - startPoint.Y);
            if (endPoint.X - startPoint.X >= 0)
                Canvas.SetLeft(currentBoxSelectedBorder, startPoint.X);
            else
                Canvas.SetLeft(currentBoxSelectedBorder, endPoint.X);
            if (endPoint.Y - startPoint.Y >= 0)
                Canvas.SetTop(currentBoxSelectedBorder, startPoint.Y);
            else
                Canvas.SetTop(currentBoxSelectedBorder, endPoint.Y);
        }
       
        public static List<T> GetChildObjects<T>(System.Windows.DependencyObject obj) where T : System.Windows.FrameworkElement
        {
           DependencyObject? child = null;
            List<T> childList = new List<T>();
            for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
            {
                child = VisualTreeHelper.GetChild(obj, i);
                if (child is T)
                {
                    childList.Add((T)child);
                }
                childList.AddRange(GetChildObjects<T>(child));
            }
            return childList;
        }






        #endregion

        
    }

   
}
